'use strict';
const axios = require('axios')
const fs = require('mz/fs');
const path = require('path');
const Controller = require('egg').Controller;
const svgCaptcha = require('svg-captcha')
class userController extends Controller {

    async gitlogin() {
        const { ctx } = this;
        const { code } = ctx.request.body;
        if (code) {
            const { gitee } = ctx.app.config;
            const gitToken = await axios.post('https://gitee.com/oauth/token', {
                grant_type: 'authorization_code',
                code,
                client_id: gitee.client_id,
                redirect_uri: gitee.redirect_uri,
                client_secret: gitee.client_secret,
            })
            if (gitToken.status == 200) {
                const access_token = gitToken.data.access_token;
                const userInfo = await axios.get('https://gitee.com/api/v5/user', {
                    params: {
                        access_token
                    }
                })
                if (userInfo.status === 200) {
                    ctx.body = {
                        code: 200,
                        msg: "第三方登陆场成功",
                        userInfo: userInfo.data
                    }
                } else {
                    ctx.body = {
                        code: 200,
                        msg: "第三方登陆场失败,未请求到用户信息",
                    }
                }
            } else {
                ctx.body = {
                    code: 402,
                    msg: "token请求失败",
                }
            }
        } else {
            ctx.body = {
                code: 402,
                msg: "code请求失败",
            }
        }
    }
    async captcha() {
        const { ctx } = this;
        let captcha = svgCaptcha.create();
        ctx.session.captcha = captcha.text;
        ctx.type = 'svg';
        ctx.body = captcha.data;
    }
    async email() {
        let captcha = this.ctx.session.captcha;
        const mailOptions = {
            from: '15367395540@163.com',
            to: '431884088@qq.com',
            subject: 'hello world',
            html: `<h1>验证${captcha}</h1>`
        };

        this.app.email.sendMail(mailOptions, (error, response) => {
            if (error) {
                this.ctx.body = {
                    code: 200,
                    smg: "小伙子不行啊"
                }
            } else {
                this.ctx.body = {
                    code: 200,
                    smg: "啊孙红岩好帅"
                }
            }
            this.app.email.close();
        });
        this.ctx.body = {
            code: 202,
            smg: "啊孙红岩好帅"
        }
    }
    async upload() {
        const ctx = this.ctx;
        const file = ctx.request.files[0];
        const name = 'egg-oss-demo/' + path.basename(file.filename);
        let result;
        try {
            result = await ctx.oss.put(name, file.filepath);
            ctx.body = {
                code:401,
                msg:"卧槽上传成功"
            }
        } finally {
            await fs.unlink(file.filepath);
            ctx.body = {
                code:200,
                msg:"卧槽上传成功"
            }
        }
        ctx.body = {
            code:200,
            msg:"woc",
            result
        }
    }
}

module.exports = userController;
